package cn.com.webtools.servlet;

import java.io.IOException;
import java.text.DecimalFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mixsmart.utils.StringUtils;

import cn.com.webtools.bean.PPIBean;
import cn.com.webtools.service.IPPIService;
import cn.com.webtools.service.impl.PPIServiceImpl;

/**
 * 用于计算屏幕PPI
 * @author lmq <br />
 * 2016年11月24日
 * @version 1.0
 * @since 1.0
 */
@WebServlet("/ppi")
public class PPIServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	private IPPIService ppiServ;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PPIServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		setThemeSkins(request);
		double screenHeight = 1920;
		double screenWidth = 1080;
		double screenSize = 5;
		computeShow(screenHeight, screenWidth, screenSize, request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		setThemeSkins(request);
		String screenHeightStr = request.getParameter("screenHeight");
		String screenWidthStr = request.getParameter("screenWidth");
		String screenSizeStr = request.getParameter("screenSize");
		String normalRP = request.getParameter("normalRP");
		if(StringUtils.isNotEmpty(normalRP)) 
			request.setAttribute("normalRP", normalRP);
		String msg = "输入项有误";
		if(StringUtils.isNotEmpty(screenHeightStr) && StringUtils.isNum(screenHeightStr.trim()) 
				&& StringUtils.isNotEmpty(screenWidthStr) && StringUtils.isNum(screenWidthStr.trim()) 
				&& StringUtils.isNotEmpty(screenSizeStr) && StringUtils.isNum(screenSizeStr.trim())) {
			double screenHeight = Double.parseDouble(screenHeightStr.trim());
			double screenWidth = Double.parseDouble(screenWidthStr.trim());
			double screenSize = Double.parseDouble(screenSizeStr.trim());
			computeShow(screenHeight, screenWidth, screenSize, request, response);
		} else {
			PPIBean ppiBean = new PPIBean(screenHeightStr, screenWidthStr, screenSizeStr, "");
			request.setAttribute("ppiBean", ppiBean);
			request.setAttribute("msg", msg);
			request.getRequestDispatcher(BASE_VIEW_DIR+"ppi.jsp").forward(request, response);
		}
	}
	
	private void computeShow(double screenHeight, double screenWidth, double screenSize, 
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(null == ppiServ) {
			ppiServ = new PPIServiceImpl();
		}
		DecimalFormat df = new DecimalFormat("###.##");
		PPIBean ppiBean = new PPIBean(String.valueOf(screenHeight), String.valueOf(screenWidth), String.valueOf(screenSize), "");
		try {
			double ppi = ppiServ.compute(screenHeight, screenWidth, screenSize);
			String ppiStr = df.format(ppi);
			ppiBean.setPpi(ppiStr);
		} catch(Exception ex) {
			request.setAttribute("msg", "参数输入有误");
		}
		request.setAttribute("ppiBean", ppiBean);
		request.getRequestDispatcher(BASE_VIEW_DIR+"ppi.jsp").forward(request, response);
	}
	
	private void setThemeSkins(HttpServletRequest request) {
		request.setAttribute("theme", "default");
		request.setAttribute("skins", "primary");
	}
	
}
